package com.cuit.freshmanMS.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.freshmanMS.domain.Admin;
import com.cuit.freshmanMS.mapper.AdminMapper;
import com.cuit.freshmanMS.service.AdminService;
import com.cuit.freshmanMS.util.StringUtil;
import com.cuit.freshmanMS.util.result.CommonResult;
import com.cuit.freshmanMS.util.result.PageResult;
import com.cuit.freshmanMS.util.result.ResultCode;
import com.cuit.freshmanMS.util.result.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @description:
 * @author: ziger
 * @time: 2024/1/17 10:04
 */

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public CommonResult<Boolean> login(Admin admin) {
        //错误检测 用户名和密码不能为空
        if(StringUtil.isNull(admin.getUsername(), admin.getPassword())){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }


        boolean exists = adminMapper.exists(new QueryWrapper<>(admin));
        if (!exists){
            return ResultUtil.fail(ResultCode.USER_ACCOUNT_NOT_MATCH);
        }
        return ResultUtil.success(exists);
    }


    @Override
    public PageResult<Admin> selectAll(Integer currentPage, Integer pageSize, String keyword) {
        // 错误检测
        if (currentPage == null || pageSize == null){
            return new PageResult<>();
        }
        if (currentPage <= 0 || pageSize <= 0){
            return new PageResult<>();
        }


        Page<Admin> resultPage = adminMapper.selectPage(
                new Page<>(currentPage, pageSize),
                new QueryWrapper<>(
                        new Admin()).like("username", keyword));

        return new PageResult<>(resultPage);

    }

    @Override
    public CommonResult add(Admin admin) {
        // 错误检测
        if (StringUtil.isNull(admin.getUsername(), admin.getPassword())){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }

        //用户名不能重复
        boolean exists = adminMapper.exists(new QueryWrapper<>(
                new Admin(null,admin.getUsername(),null)));
        if (exists){
            return ResultUtil.fail(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
        }

        adminMapper.insert(admin);
        return ResultUtil.success();
    }

    @Override
    public CommonResult update(Admin admin) {
        // 错误检测
        if (admin.getId() == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtil.isNull(admin.getUsername(), admin.getPassword())){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        //如果用户名被修改,需要检测用户名是否重复
        Admin old = adminMapper.selectById(admin.getId());
        if (!old.getUsername().equals(admin.getUsername())){
            boolean exists = adminMapper.exists(new QueryWrapper<>(
                    new Admin(null,admin.getUsername(),null)));
            if (exists){
                return ResultUtil.fail(ResultCode.USER_ACCOUNT_ALREADY_EXIST);
            }
        }
        adminMapper.updateById(admin);
        return ResultUtil.success();
    }

    @Override
    public CommonResult delete(Integer id) {
        if (id == null){
            return ResultUtil.fail(ResultCode.PARAM_IS_BLANK);
        }
        boolean exists = adminMapper.exists(new QueryWrapper<>(new Admin(id,null, null)));
        if (!exists){
            return ResultUtil.fail(ResultCode.USER_ACCOUNT_NOT_EXIST);
        }

        adminMapper.deleteById(id);
        return ResultUtil.success();
    }

    @Override
    public CommonResult deleteBatch(List<Integer> ids) {
        Integer count = adminMapper.deleteBatchIds(ids);
        return ResultUtil.success(count);
    }
}
